package org.dromara.exam.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.exam.domain.vo.ExamBookWordRelationVo;
import org.dromara.exam.domain.bo.ExamBookWordRelationBo;
import org.dromara.exam.service.IExamBookWordRelationService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 单词本关联单词
 *
 * @author Lion Li
 * @date 2025-09-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/exam/bookWordRelation")
public class ExamBookWordRelationController extends BaseController {

    private final IExamBookWordRelationService examBookWordRelationService;

    /**
     * 查询单词本关联单词列表
     */
    @SaCheckPermission("exam:bookWordRelation:list")
    @GetMapping("/list")
    public TableDataInfo<ExamBookWordRelationVo> list(ExamBookWordRelationBo bo, PageQuery pageQuery) {
        return examBookWordRelationService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出单词本关联单词列表
     */
    @SaCheckPermission("exam:bookWordRelation:export")
    @Log(title = "单词本关联单词", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ExamBookWordRelationBo bo, HttpServletResponse response) {
        List<ExamBookWordRelationVo> list = examBookWordRelationService.queryList(bo);
        ExcelUtil.exportExcel(list, "单词本关联单词", ExamBookWordRelationVo.class, response);
    }

    /**
     * 获取单词本关联单词详细信息
     *
     * @param relationId 主键
     */
    @SaCheckPermission("exam:bookWordRelation:query")
    @GetMapping("/{relationId}")
    public R<ExamBookWordRelationVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long relationId) {
        return R.ok(examBookWordRelationService.queryById(relationId));
    }

    /**
     * 新增单词本关联单词
     */
    @SaCheckPermission("exam:bookWordRelation:add")
    @Log(title = "单词本关联单词", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ExamBookWordRelationBo bo) {
        return toAjax(examBookWordRelationService.insertByBo(bo));
    }

    /**
     * 修改单词本关联单词
     */
    @SaCheckPermission("exam:bookWordRelation:edit")
    @Log(title = "单词本关联单词", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ExamBookWordRelationBo bo) {
        return toAjax(examBookWordRelationService.updateByBo(bo));
    }

    /**
     * 删除单词本关联单词
     *
     * @param relationIds 主键串
     */
    @SaCheckPermission("exam:bookWordRelation:remove")
    @Log(title = "单词本关联单词", businessType = BusinessType.DELETE)
    @DeleteMapping("/{relationIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] relationIds) {
        return toAjax(examBookWordRelationService.deleteWithValidByIds(List.of(relationIds), true));
    }
}
